<?php
/**
 * Created by PhpStorm.
 * User: chengchaogui
 * Date: 2017/8/11
 * Time: 10:51
 */

namespace App\Repository\Service;


use App\Framework\Config\SystemConstant;
use App\Models\Log\ManagerHandleLog;
use App\Models\Log\ManagerLoginLog;
use App\Repository\Contract\IRzjhpLog;

class RzjhpLogService implements IRzjhpLog
{
    /**
     * 保存操作日志
     * @param $manager_id
     * @param $username
     * @param $handle_ip
     * @param $handle_url
     * @param $level
     * @param $description
     * @param $handle_time
     * @return mixed
     */
    public function storeHandleLog($manager_id, $username, $handle_ip, $handle_url, $level, $description, $handle_time)
    {
        $log = new ManagerHandleLog();
        $log->manager_id = $manager_id;
        $log->username = $username;
        $log->handle_ip = $handle_ip;
        $log->handle_url = $handle_url;
        $log->level = $level;
        $log->description = $description;
        $log->handle_time = $handle_time;

        $log->save();
    }

    /**
     * 获取操作日志
     * @param $form
     * @return mixed
     */
    public function getHandleLog($form)
    {
        $logs = ManagerHandleLog::where(function ($query) use ($form) {
            if (!empty($form->managerID)) {
                $query->where('manager_id', $form->managerID);
            }

            if (!empty($form->username)) {
                $query->where('username', $form->username);
            }
        })->orderBy('manager_handle_log_id', 'desc')->paginate(SystemConstant::ADMIN_TABLE_ROWS);

        return $logs;
    }

    /**
     * 保存登录日志
     * @param $manager_id
     * @param $username
     * @param $result
     * @param $login_ip
     * @param $login_time
     * @return mixed
     */
    public function storeLoginLog($manager_id, $username, $result, $login_ip, $login_time)
    {
        $log = new ManagerLoginLog();
        $log->manager_id = $manager_id;
        $log->username = $username;
        $log->result = $result;
        $log->login_ip = $login_ip;
        $log->login_time = $login_time;

        $log->save();
    }

    /**
     * 获取登录日志
     * @param $form
     * @return mixed
     */
    public function getLoginLog($form)
    {
        $logs = ManagerLoginLog::where(function ($query) use ($form) {
            if (!empty($form->managerID)) {
                $query->where('manager_id', $form->managerID);
            }
            if (!empty($form->username)) {
                $query->where('username', $form->username);
            }
        })->orderBy('manager_login_log_id', 'desc')->paginate(SystemConstant::ADMIN_TABLE_ROWS);

        return $logs;
    }
}